﻿using AvalonDock.Layout;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using 雷赛板卡上位机.Models;
using 雷赛板卡上位机.Services;
using 雷赛板卡上位机.View;

namespace 雷赛板卡上位机.ViewModel
{
    public partial class SideberViewModel : BaseViewModel
    {
        // 观察属性，用于存储轴项树数据
        [ObservableProperty]
        ObservableCollection<AxisItemTreeData> axes;

        // 构造函数，初始化视图模型
        public SideberViewModel() : base()
        {
            List<AxisItemTreeData> treeDatas = new List<AxisItemTreeData>();
            // 添加根节点 "参数设置"
            treeDatas.Add(new AxisItemTreeData() { Id = 1, StepId = 1, ParentId = 0, Name = "参数设置" });
            // 添加根节点 "代码执行"
            treeDatas.Add(new AxisItemTreeData() { Id = 2, StepId = 1, ParentId = 0, Name = "代码执行" });

            Axes = new ObservableCollection<AxisItemTreeData>(treeDatas);

        }

        // 树视图点击事件处理方法
        [RelayCommand]
        void ClickTreeView(object s)
        {
            AxisItemTreeData axisItem = s as AxisItemTreeData;
            //TabItem? item = default;
            LayoutDocument? item = default(LayoutDocument);

            if (axisItem == null)
            {
                return;
            }

            switch (axisItem.StepId)
            {
                case 1:
                    if (axisItem.Name == "代码执行")
                        item = ControlService.GetViewModel<InputTextBoxViewModel>("代码执行区");
                    break;
                case 3:
                    item = ControlService.GetViewModel<AxisControlViewModel>(axisItem.Name, axisItem.Id);
                    break;

            }

            // 发送 TabItem 消息
            if (item != default && item != null)
            {
                item.IsActive = true;
                Messenger?.Send<LayoutDocument, string>(item, nameof(TabItem));
            }
        }
        /// <summary>
        /// 添加軸視圖
        /// </summary>
        /// <param name="index"></param>
        private void AddView(int index)
        {
            var r = this;
            if (index != 0)
            {
                if (r.Axes[0].Children.Count != 0)
                {
                    if (r.Axes[0].Children[0].Name == $"轴控制")
                    {
                        if (r.Axes[0].Children[0].Children.Count == index)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    var x = new AxisItemTreeData() { Id = 0, StepId = r.Axes[0].StepId + 1, ParentId = r.Axes[0].StepId, Name = $"轴控制" };
                    r.Axes[0].Children.Add(x);
                }

                // 根据轴的数量添加子节点
                for (int i = 0; i < index; i++)
                {


                    var t = new AxisItemTreeData()
                    { Id = i, StepId = r.Axes[0].Children[0].StepId + 1, ParentId = r.Axes[0].Children[0].StepId, Name = $"轴{i}" };
                    r.Axes[0].Children[0].Children.Add(t);
                }
            }
        }
        protected override void OnActivated()
        {
            Messenger?.Register<SideberViewModel, ValueChangedMessage<bool>, string>(this, "AxisInit",
                (r, m) =>
                {
                    if (m.Value)
                    {
                        int index = Messenger.Send<RequestMessage<int>, string>("AxisNum");
                        AddView(index);
                    }
                });

            Messenger?.Register<SideberViewModel, ValueChangedMessage<bool>, string>(this, "MenuClose", (r, m) =>
            {
                if (m.Value)
                {
                    r.Axes[0].Children.Clear();
                }
            });
        }
    }
}